﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using ICSharpCode.TextEditor;
using ICSharpCode.TextEditor.Gui;
using ICSharpCode.TextEditor.Document;
using ICSharpCode.TextEditor.Actions;
using RkLib.WinApi;
using RkLib.Configuration;
namespace CodeCollection
{
    public class CodeEditor
    {
        private string CurFilePath;
        private IntPtr MsgHwnd = IntPtr.Zero;
        TextEditorControl EditorCtrl = null;

        public bool TextChanged { get; protected set; }
        public bool Saved { get; protected set; }
        public CodeEditor(object editor, IntPtr msgHwnd)
        {
            MsgHwnd = msgHwnd;
            EditorCtrl = (TextEditorControl)editor;
            EditorCtrl.Encoding = Encoding.Default;
            EditorCtrl.Document.HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategy("VBNET");
            EnableTextChange(true);
            //EditorCtrl.KeyDown += new System.Windows.Forms.KeyEventHandler(EditorCtrl_KeyDown);
        }

        public void DisplayCode(string filePath)
        {
            if (!Saved)
            {
                SaveToFile();
            }
            if (!File.Exists(filePath))
            {
                Win32Api.SendMessage(MsgHwnd, (int)WM_MSG.WM_TV_REFRESH, IntPtr.Zero, IntPtr.Zero);
                return;
            }
            EnableTextChange(false);
            EditorCtrl.Text = PublicFunction.ReadCodeFromFile(filePath);
            //EditorCtrl.LoadFile(filePath,false,true);
            EditorCtrl.Refresh();
            EnableTextChange(true);
            CurFilePath = filePath;
            Cfg.WriteConfig(gValue.SFT_SEC_CFG, gValue.SFT_LST_DIS_FILE, CurFilePath);
            Win32Api.SendMessage(MsgHwnd, (int)WM_MSG.WM_EDITOR_SAVE, IntPtr.Zero, IntPtr.Zero);
        }
        public void SaveToFile()
        {
            if (null == CurFilePath)
            {
                return;
            }
            PublicFunction.WriteCodeToFile(CurFilePath,EditorCtrl.Text);
            //EditorCtrl.SaveFile(CurFilePath);
            Saved = true;
            Win32Api.SendMessage(MsgHwnd, (int)WM_MSG.WM_EDITOR_SAVE, IntPtr.Zero, IntPtr.Zero);
        }

        private void EditorCtrl_TextChanged(object sender, EventArgs e)
        {
            Win32Api.SendMessage(MsgHwnd, (int)WM_MSG.WM_EDITOR_TEXT_CHANGE, IntPtr.Zero, IntPtr.Zero);
            Saved = false;
        }

        private void EditorCtrl_KeyDown(object sender, KeyEventArgs e)
        {
            if ((!Saved) && (e.KeyData == (Keys.B | Keys.ControlKey)))
            {
                SaveToFile();
            }
        }
        private void EnableTextChange(bool enable)
        {
            if (enable)
            {
                EditorCtrl.TextChanged += new EventHandler(EditorCtrl_TextChanged);
            }
            else
            {
                EditorCtrl.TextChanged -= new EventHandler(EditorCtrl_TextChanged);
            }
        }
    }
}
